package com.ncepu.clubmanagement.controller;

import com.ncepu.clubmanagement.anno.CommitteePermission;
import com.ncepu.clubmanagement.exception.AccessDeniedException;
import com.ncepu.clubmanagement.pojo.*;
import com.ncepu.clubmanagement.service.ClubService;
import com.ncepu.clubmanagement.service.PersonService;
import com.ncepu.clubmanagement.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/club")
public class ClubController {

    @Autowired
    private ClubService clubService;

    @Autowired
    private JwtUtils jwtUtil;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private PersonService personService;

    @PostMapping()
    @CommitteePermission
    public Result addClub(@RequestBody ClubAdd clubAdd){
        log.info("添加社团");
        clubService.addClub(clubAdd);
        return Result.success();
    }

    @GetMapping()
    public Result getClub(String clubName, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10")Integer pageSize){
        log.info("查询社团");
        PageBean clubs = clubService.getClub(page, pageSize, clubName);
        return Result.success(clubs);
    }

    @GetMapping("/joined/self")
    public Result getJoinedClub(String clubName, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10")Integer pageSize){
        log.info("查询参与的社团");

        // 先获取个人信息
        String jwt = request.getHeader("token");
        Claims claims = jwtUtil.parseJWT(jwt);
        Integer personId = (Integer) claims.get("id");

        // 查询社团信息
        List<Club> clubs = clubService.getJoinedClubWithoutPage(clubName,personId);
        return Result.success(clubs);

    }

    @GetMapping("/joined")
    public Result getJoinedClub(String clubName, @RequestParam(name = "personId",required = true) Integer targetPersonId) throws AccessDeniedException {
        log.info("查询{}参与的社团",targetPersonId);
        //1. 进行分析：此次访问是否合法
        String jwt = request.getHeader("token");
        Claims claims = jwtUtil.parseJWT(jwt);
        Integer sourcePersonId = (Integer) claims.get("id");
        // 调用服务层分析合法性
        boolean permission = personService.checkIsManaged(sourcePersonId, targetPersonId);
        if(! permission){
            throw new AccessDeniedException();
        }
        //2. 调用服务开始实际查询信息
        List<Club> ls = clubService.getJoinedClubWithoutPage(clubName, targetPersonId);

        return Result.success(ls);

    }


    @GetMapping("/managed/self")
    public Result getManagedClub(String clubName, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10")Integer pageSize){
        log.info("查询管理的社团");

        // 先获取个人信息
        String jwt = request.getHeader("token");
        Claims claims = jwtUtil.parseJWT(jwt);
        Integer personId = (Integer) claims.get("id");

        // 查询社团信息
        List<Club> clubs = clubService.getManagedClubWithoutPage(clubName,personId);

        return Result.success(clubs);
    }

    @GetMapping ("/ChangeableInfo")
    public Result getClubInfo(Integer clubId){
        log.info("查询{}社团信息",clubId);
        ClubChangeableInfoWithManager clubInfo = clubService.getClubChangeableInfoWithoutManager(clubId);
        return Result.success(clubInfo);
    }

    @PutMapping()
    public Result changeClubInfo(ClubModify clubModify, @RequestParam(value = "image", required = false) MultipartFile image) throws Exception {
        log.info("修改社团信息");
        clubService.updateClubInfo(clubModify,image);
        return Result.success();
    }

    @DeleteMapping()
    public Result deleteClub(@RequestParam(name = "id") Integer clubId){
        log.info("删除社团");
        clubService.deleteClub(clubId);
        return Result.success();
    }



}




